package com.bbva.kyfb.kyfbjpatlsb.dominiotlsb;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

/**
 * The Class TtlsbgecDataOnDemand.
 */
@Component
@Configurable
@RooDataOnDemand(entity = Ttlsbgec.class)
public class TtlsbgecDataOnDemand {

	/** The rnd. */
	private Random rnd = new SecureRandom();

	/** The data. */
	private List<Ttlsbgec> data;

	/**
	 * Gets the new transient ttlsbgec.
	 *
	 * @param index the index
	 * @return the new transient ttlsbgec
	 */
	public Ttlsbgec getNewTransientTtlsbgec(int index) {
        Ttlsbgec obj = new Ttlsbgec();
        setEmbeddedIdClass(obj, index);
        setAudFmodific(obj, index);
        setAudUsuario(obj, index);
        setDesRegiswb(obj, index);
        return obj;
    }

	/**
	 * Sets the embedded id class.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setEmbeddedIdClass(Ttlsbgec obj, int index) {
        TtlsbgecPK embeddedIdClass = new TtlsbgecPK("codRegistw_" + index, new Integer(index).shortValue(), new Integer(index).shortValue(), new Integer(index).shortValue());
        obj.setId(embeddedIdClass);
    }

	/**
	 * Sets the aud fmodific.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudFmodific(Ttlsbgec obj, int index) {
        Date audFmodific = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setAudFmodific(audFmodific);
    }

	/**
	 * Sets the aud usuario.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudUsuario(Ttlsbgec obj, int index) {
        String audUsuario = "audUsu_" + index;
        if (audUsuario.length() > 8) {
            audUsuario = audUsuario.substring(0, 8);
        }
        obj.setAudUsuario(audUsuario);
    }

	/**
	 * Sets the des regiswb.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesRegiswb(Ttlsbgec obj, int index) {
        String desRegiswb = "desRegiswb_" + index;
        if (desRegiswb.length() > 500) {
            desRegiswb = desRegiswb.substring(0, 500);
        }
        obj.setDesRegiswb(desRegiswb);
    }

	/**
	 * Gets the specific ttlsbgec.
	 *
	 * @param index the index
	 * @return the specific ttlsbgec
	 */
	public Ttlsbgec getSpecificTtlsbgec(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Ttlsbgec obj = data.get(index);
        return Ttlsbgec.findTtlsbgec(obj.getId());
    }

	/**
	 * Gets the random ttlsbgec.
	 *
	 * @return the random ttlsbgec
	 */
	public Ttlsbgec getRandomTtlsbgec() {
        init();
        Ttlsbgec obj = data.get(rnd.nextInt(data.size()));
        return Ttlsbgec.findTtlsbgec(obj.getId());
    }

	/**
	 * Modify ttlsbgec.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	public boolean modifyTtlsbgec(Ttlsbgec obj) {
        return false;
    }

	/**
	 * Inits the.
	 */
	public void init() {
        data = Ttlsbgec.findTtlsbgecEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Ttlsbgec' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlsb.dominiotlsb.Ttlsbgec>();
        for (int i = 0; i < 10; i++) {
            Ttlsbgec obj = getNewTransientTtlsbgec(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
